package com.controller;

import com.dto.BaseDto;
import com.entity.User;
import com.entity.UserExample;
import com.page.PageInfo;
import com.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.List;

@Controller
public class UserController {
	@Resource
	private UserService userService;

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	@RequestMapping("user/getAll")
	public ModelAndView getAllUsers(PageInfo pageInfo) throws SQLException {
		BaseDto dto = userService.getAllUsers(pageInfo);
		return new ModelAndView("/user/select","users",dto);
	}
	@RequestMapping("/user/add")
	public BaseDto insert(User user) throws IllegalArgumentException, IllegalAccessException, SQLException{
		return userService.insertUser(user);
	}
	@RequestMapping("/user/getUserById/{uid}")
	public ModelAndView getUserByIdInfo(@PathVariable("uid") int uid)throws SQLException {
	    BaseDto dto = userService.getUserById(uid);
	    return new ModelAndView("user/update","users",dto.getT());
	}
	@RequestMapping("/user/update")
	@ResponseBody
	public BaseDto update(User user) throws IllegalArgumentException, IllegalAccessException, SQLException, NoSuchFieldException, SecurityException{
		return userService.updateUser(user);
	}
	@RequestMapping("/user/deleteById/{uid}")
	public ModelAndView deleteById(@PathVariable("uid") int uid,PageInfo pageInfo) throws IllegalArgumentException, IllegalAccessException, SQLException, NoSuchFieldException, SecurityException{
		userService.delateUser(uid);
		BaseDto dto = userService.getAllUsers(pageInfo);
		return new ModelAndView("/user/select","users",dto);
	}
}
